﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PayWayAPI.TenPayWay;

namespace CNVP.JShop.Web.Member
{
    public partial class Tenpay_Return_Immediate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //密钥
            string key = "";
            var model_Col = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(31);//  ID=31 是财富通
            if (model_Col != null)
            {
                //密钥
                key = model_Col.PayKey;
            }

            //创建PayResponseHandler实例
            PayResponseHandler resHandler = new PayResponseHandler(Context);

            resHandler.setKey(key);

            string errmsg = "";
            //判断签名
            if (resHandler.isTenpaySign())
            {
                //交易单号
                string transaction_id = resHandler.getParameter("transaction_id");

                //商户订单号
                string sp_billno = resHandler.getParameter("sp_billno");

                //金额金额,以分为单位
                decimal total_fee = Convert.ToDecimal(resHandler.getParameter("total_fee")) / 100;

                //支付结果
                string pay_result = resHandler.getParameter("pay_result");

                if ("0".Equals(pay_result))
                {
                    //买家付款成功，注意判断订单是否重复的逻辑
                    Model.CNVP_Order model = new Model.CNVP_Order();
                    model.OrderNum = sp_billno;
                    model.OrderState = 1;//JShop系统更新为‘准备发货(到付或已付)’
                    model.PayWay = "31";
                    new BLL.CNVP_Order().UpdateOrderStateByNo(model);

                    //支付成功，注意判断订单是否重复的逻辑
                    errmsg += "<img src=\"../images/pay_success.gif\" />";
                    errmsg += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    errmsg += "<font size=\"5\" color=\"blue\">支付成功!</font>";
                    errmsg += "<br/>";
                    errmsg += "<br/>";
                    errmsg += "商家定单号:" + sp_billno + "<br>金额：" + total_fee.ToString() + "元(<a href =\"OrderList.aspx\">根据此定单号进行查询</a>)" + "<br/>";

                }
                else
                {
                    //当做不成功处理
                    errmsg += "<img src=\"../images/pay_error .gif\" />";
                    errmsg += "<font size=\"5\" color=\"red\">支付失败!</font>";
                    errmsg += "<br/>";
                    errmsg += "<br/>";
                    errmsg += "------------------------------------------";
                    errmsg += "请在财富通中查看是否支付成功。<br>成功后与客服联系！";
                    errmsg += "------------------------------------------";
                }

            }
            else
            {
                errmsg += "<img src=\"../images/pay_error .gif\" />";
                errmsg += "<font size=\"5\" color=\"red\">认证签名失败!</font>";
                errmsg += "<br/>";
                errmsg += "<br/>";
                errmsg += "------------------------------------------";
                errmsg += "请在财富通中查看是否支付成功。<br>成功后与客服联系！";
                errmsg += "------------------------------------------";
            }
            LabMSG.Text = errmsg;
        }
    }
}